package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<Clue> ListClues(ClueDto clueDto) {
        Page<Clue> page = this.baseMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<Clue>(page.getTotal(), page.getRecords());
    }

    @Override
    public Clue getClueById(Integer id) {
        return this.baseMapper.getClueById(id);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void falseClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        this.updateById(clue);

        ClueTrackRecord trackRecord = new ClueTrackRecord();
        trackRecord.setClueId(id);
        trackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        trackRecord.setFalseReason(markFalseClueDto.getReason());
        trackRecord.setRecord(markFalseClueDto.getRemark());
        trackRecord.setType(0);
        trackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(trackRecord);
    }

    // 添加线索转商机功能
    @Override
    public void toBusiness(Integer id) {
        // 获取完整的线索信息
        Clue clue = clueMapper.selectById(id);
        // 更新线索状态为“商机”
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        // 添加商机记录
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);//拷贝属性，
        business.setId(null);//重置ID
        business.setUserId(null);//重置用户ID
        business.setNextTime(null);//重置下次联系时间
        business.setClueId(id);//重置线索ID
        business.setStatus(1);//重置状态
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert(business);

    }

    @Override
    public PageResult<Clue> ListCluePool(ClueDto clueDto) {
        Page<Clue> page = new Page<>(clueDto.getPage(),clueDto.getPageSize());
        Page<Clue> pageResult = clueMapper.ListCluePool(page,clueDto);
        return new PageResult<Clue>(pageResult.getTotal(),pageResult.getRecords());

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackClue(Clue clue) throws Exception {
        // 更新线索状态为“跟进中”（假设状态值为3）
        clue.setStatus(3); // 跟进中
        clue.setUpdateTime(LocalDateTime.now()); // 更新时间
        this.updateById(clue);
        // 添加跟进记录
        ClueTrackRecord trackRecord = new ClueTrackRecord();
        trackRecord.setClueId(clue.getId());
        trackRecord.setUserId(CurrentUserHoler.getCurrentUser()); //当前登录用户ID
        trackRecord.setSubject(clue.getSubject());
        trackRecord.setLevel(clue.getLevel());
        trackRecord.setRecord(clue.getRecord());
        trackRecord.setNextTime(clue.getNextTime());
        trackRecord.setType(1); // 正常跟进
        trackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(trackRecord);
    }
}
